<?php

namespace App\Http\Controllers\Admin;

use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redirect;
use App\Supervisor;
use App\Student;
use App\User;
use Excel;

class TwowayChoiceController extends Controller
{

    public function index($onlyUnselectedStudents = 1) {
        if ($onlyUnselectedStudents == 1) {
            $students = Student::whereDoesntHave('supervisors', function($query) {
                $query->where("isApproved", 1);
            })->paginate(15);
        } else {
            $users = User::where('role', 1)->paginate(15);
        }
        $user = \Auth::user();
        return view('admin.twowaychoice', compact("users", "user", "onlyUnselectedStudents", "students"));
    }

    public function edit(Request $request)
    {
    	$user = \Auth::user();
    	
        if ($request->input('dispatch') == 1) {
            $students = Student::all();
            $dispatch = 1;
        } else {
            $students = Student::whereDoesntHave('supervisors', function($query) {
                    $query->where("isApproved", 1);
            })->get();
            $dispatch = 0;
        }
        //$students = Student::all();
    	$supervisors = Supervisor::all();
        foreach ($supervisors as $supervisor) {
            $supervisor->residue = $supervisor->quota - $supervisor->students()->wherePivot('isApproved', true)->count();
        }
        $edit = "";
    	return view('admin.enforcechoice', compact("supervisors", "user", "students", "edit", "dispatch"));
    }

    public function export()
    {
        $th = array("学生学号", "学生姓名", "学生班级", "学生专业", "学生手机号码", "导师工号", "导师姓名", "导师手机号码");
        $data = array();
        $data[] = $th;
        $students = Student::all();

        foreach ($students as $student) {
            $supervisor = $student->supervisors()->wherePivot('isApproved', true)->get()->first();
            $row = array($student->user->username, $student->user->name, $student->class, $student->major, $student->user->phone);
            if ($supervisor != null) {
                $row[] = $supervisor->user->username;
                $row[] = $supervisor->user->name;
                $row[] = $supervisor->user->phone;
            }
            $data[] = $row;
        }
        Excel::create('2017届毕业生双选结果', function($excel)  use($data) {

            $excel->sheet('2017届毕业生双选结果', function($sheet)  use($data) {

                $sheet->setOrientation('landscape');
                $sheet->fromArray($data);
            });

        })->export('xls');
    }

    public function store(Request $request)
    {
    	$supervisor = Supervisor::find($request->input("supervisor"));
    	if ($supervisor != null) {
      		$student_ids = $request->input('students');  		
            if (is_array($student_ids)) {
          		foreach ($student_ids as $student_id) {
                   $student = Student::find($student_id);
                   $student->supervisors()->sync([$request->input("supervisor") => array("isApproved" => true)]);
          		}
            }
            $edit = "分配成功";
        } else {
            $edit = "分配失败";
        }

        $user = \Auth::user();
        $dispatch = $request->input('dispatch');
        if ($dispatch == 1) {
            $students = Student::all();
        } else {
            $students = Student::whereDoesntHave('supervisors', function($query) {
                $query->where("isApproved", 1);
            })->get();
        }
        $supervisors = Supervisor::all();
        foreach ($supervisors as $supervisor) {
            $supervisor->residue = $supervisor->quota - $supervisor->students()->wherePivot('isApproved', true)->count();
        }
        
       
    	return view('admin.enforcechoice', compact("supervisors", "user", "students", "edit", "dispatch"));
    }

}

class ScanInsertRowAPIView(generics.CreateAPIView, generics.GenericAPIView):
    def create(self, request, *args, **kwargs):
        json_result = json.loads(request.body)
        sh = gc.open(settings.GOOGLE_SHEET_FILE_NAME)
        worksheet = sh.get_worksheet(0)
        worksheet.insert_row([json_result["Brand"], json_result["Model"], json_result["Short Descriptioin"],
             json_result["Price"], json_result["If Item on sale"], json_result["Tag"], settings.GOOGLE_DRIVE_FOLDER_URL + json_result['folder_id']])
        return JsonResponse({'ret': 0, 'folder_id': json_result["folder_id"]})
